package com.trkj.zzplus.ui.news;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.trkj.zzplus.R;
import com.trkj.zzplus.ui.tools.SetRoundImg;

/**
 * Created by Administrator on 2016/6/7.
 */
public class NewsCommentsActivity extends AppCompatActivity {

    //private Toolbar mToolBar;
    // private TextView mToolBarTextView;
    private ListView lv_HotComments;
    private ListViewForScrollView lv_LastComments;
    private ScrollView mScrollView;

    //底部评论
    private LinearLayout comment1;
    private LinearLayout comment2;

    //private ImageView ImgBack;
    private ImageView userLogo;
    private ImageView likeBttn;

    private Toolbar mToolbar;
    private TextView mToolBarTextView;

    //评论xml组件
    private ImageView commit;
    private ImageView cancel;
    private EditText content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_comments);
        initViews();
        //    initTopBar();
        initToolBar();
        initEvents();
        //    initToolbar();
    }

    private void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title);
        setSupportActionBar(mToolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        mToolbar.setNavigationIcon(R.mipmap.btn_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mToolBarTextView.setText("评论");
    }

    private void initViews() {
        lv_HotComments = (ListView) findViewById(R.id.news_comments_lv_hotCom);
        lv_LastComments = (ListViewForScrollView) findViewById(R.id.news_comments_lv_lastCom);
        mScrollView = (ScrollView) findViewById(R.id.news_comments_scrollView);
        //手动滚动至顶部
        mScrollView.smoothScrollTo(0, 0);

        comment1 = (LinearLayout) findViewById(R.id.news_comments_send);
        comment2 = (LinearLayout) findViewById(R.id.news_comments_sentShow);
        comment2.setVisibility(View.GONE);

        lv_HotComments.setAdapter(new CommentsAdapter(true));
        lv_LastComments.setAdapter(new CommentsAdapter());

        commit = (ImageView) findViewById(R.id.com_commit);
        cancel = (ImageView) findViewById(R.id.com_cancel);
        content = (EditText) findViewById(R.id.com_content);
    }

    /* private void initTopBar() {
         ImgBack = (ImageView) findViewById(R.id.ui_goBack);
         ImgBack.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 onBackPressed();
             }
         });
     }
 */
    private void initEvents() {

        lv_HotComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NewsCommentsActivity.this, NewsCommentsDetailActivity.class);
                intent.putExtra("position", "第" + position + "个评论");
                startActivity(intent);
            }
        });

        lv_LastComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NewsCommentsActivity.this, NewsCommentsDetailActivity.class);
                intent.putExtra("position", "第" + position + "个评论");
                startActivity(intent);
            }
        });

        comment1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment1.setVisibility(View.GONE);
                comment2.setVisibility(View.VISIBLE);
            }
        });

        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //提交评论
                Toast.makeText(NewsCommentsActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                comment2.setVisibility(View.GONE);
                comment1.setVisibility(View.VISIBLE);
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment2.setVisibility(View.GONE);
                comment1.setVisibility(View.VISIBLE);
            }
        });
    }

    class CommentsAdapter extends BaseAdapter {
        public int hotComNum = 3;
        public boolean isHot;


        public CommentsAdapter(boolean isHot) {
            this.isHot = isHot;
        }

        public CommentsAdapter() {
        }

        @Override
        public int getCount() {
            if (isHot == true)
                return hotComNum;      //热评数目，固定为hotComNum;
            else
                return 10;         //评论数目
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(NewsCommentsActivity.this).inflate(R.layout.news_comments_item, null);
            userLogo = (ImageView) view.findViewById(R.id.news_comments_userLogo);

            //设置圆形头像
            userLogo.setImageBitmap(
                    new SetRoundImg().makeRoundCorner(
                            BitmapFactory.decodeResource(getResources(),
                                    R.mipmap.test_userlogo
                            )));
            return view;
        }
    }
}
